<?php
/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

    
function get_page_link($inpage, $text){
    global $page;
    $xtpl = new XTemplate('home.html',PAGE_PATH.DS.DS.$page.DS.DS.'tpl');
    $xtpl->assign("PAGE", $inpage);
    $xtpl->assign("PAGETEXT", $text);
    $xtpl->parse( 'topic_list_page_link' );
    return $xtpl->text('topic_list_page_link');
    
}


function get_page_nav_html($inpage, $count){
    $re = "Page ".$inpage." of ".$count." [".get_page_link(1, 1);
    if ($inpage>2){
        $re = $re." ".get_page_link($inpage-1, $inpage-1);
    }
    if ($inpage>1){
        $re = $re." ".$inpage;
    }

    if ($inpage<$count){
        $re = $re." ".get_page_link($inpage+1, $inpage+1);
    }

    if ($inpage+1 < $count){
        $re = $re." ".get_page_link($count, "LAST");
    }

    return $re." ]";
}


    function get_topic_info_html($alt, $topicInfor){
            $userId = $_SESSION['id'];
            global $page, $template2, $user, $topic;
            $xtpl = new XTemplate('topics_info.html',PAGE_PATH.DS.DS.$page.DS.DS.'tpl');
            $html_class = "table_row_alt".$alt;
            $edit_img = LAYOUT_URL."/images/edit.png";
            $id=$topicInfor["id"];
            $topicname=$topicInfor["name"];
            $advisername=$user->get_display_name($topicInfor["giaovien_id"]);            
            $infor=$topicInfor["thongtin_detai"];
            $amountReg=$topic->get_students_count($id);
            $infoUser = $user->get_user_info($userId);

            $xtpl->assign('html_class', $html_class);
            $xtpl->assign('id', $id);
            $xtpl->assign('name', $topicname);
            $xtpl->assign('adviser_name', $advisername);
            $xtpl->assign('thongtin_detai', $infor);            

            if (have_permission($groupid=2)){
                $status = status($infoUser["detai_id"],$topicInfor["id"],$topicInfor["status"]
                    ,$topicInfor["accept_student"],$userId);
                $xtpl->assign('status',$status);
            }

            if($topicInfor["giaovien_id"]==$userId ){
                if ($amountReg>0){
                    $xtpl->assign('accept_link', '<a href="'.ABS_URL.'?page=topic1&act=acceptTopic&topicId='.$id.'">'.$amountReg.'</a>');
                }else {
                    $xtpl->assign('std_count',$amountReg);
                }
                $xtpl->assign('access_link', '<a href="'.ABS_URL.'?page=topic1&act=editTopic&topicId='.$id.'"><img style="margin: 4px; border:0" src="'.$edit_img.'"></a>');
            }else $xtpl->assign('std_count', $amountReg);

            $ext_content = "";
            if (have_permission($groupid=1)){
                $xtpl->parse( 'sgv2' );
                $ext_content = $xtpl->text('sgv2');
            }else if (have_permission($groupid=2)){
                $xtpl->parse( 'sgv2' );
                $ext_content = $xtpl->text('sgv2');
            }
            $xtpl->assign('EXT2', $ext_content);
            
            $xtpl->parse( 'topic' );
            $topic_content= $xtpl->text('topic');
            return $topic_content;
    }

 function get_sv_info_html($alt, $svInfor,$topicId){
            global $page, $template2, $user, $topic;
            $userId = $_SESSION['id'];
            $xtpl = new XTemplate('sv_info.html',PAGE_PATH.DS.DS.$page.DS.DS.'tpl');
            $html_class = "table_row_alt".$alt;
            $edit_img = LAYOUT_URL."/images/tick.jpg";
            $reject_img = LAYOUT_URL."/images/delete.jpg";
            $id=$svInfor["user_id"];
            $dht=$svInfor["diemhoctap"];
            $drl=$svInfor["diemrenluyen"];
            $info=$svInfor["sinhvien_info"];
            $infoTopic = $topic->get_topic_info($topicId);
            
            $xtpl->assign('html_class', $html_class);
            $xtpl->assign('name', $user->get_name_by_id($id));
            $xtpl->assign('dht', $dht);
            $xtpl->assign('drl', $drl);
            $xtpl->assign('info', $info);
//            if ($infoTopic["status"]==2 &&$infoTopic["accept_student"]==$id){
               // $xtpl->assign('access_link', '<a href="'.ABS_URL.'?page=topic1&act=ok&topicId='.$topicId.'&svId='.$id.'"><img style="margin: 4px; border:0" src="'.$reject_img.'"></a>');
//            }
//            else
            if ($infoTopic["status"]<2){
                $xtpl->assign('access_link', '<a href="'.ABS_URL.'?page=topic1&act=ok&topicId='.$topicId.'&svId='.$id.'"><img style="margin: 4px; border:0" src="'.$edit_img.'"></a>');
            }
            else if ($infoTopic["status"]==2){
                if ($infoTopic["accept_student"]==$id){
                    $xtpl->assign('access_link', '<a href="'.ABS_URL.'?page=topic1&act=notok&topicId='.$topicId.'&svId='.$id.'"><img style="margin: 4px; border:0" src="'.$reject_img.'"></a>');
                }
                else $xtpl->assign('status',"Không chấp nhận!");
            }

            $xtpl->parse( 'sv' );
            $topic_content= $xtpl->text('sv');
            return $topic_content;
    }

 function status($svDetai,$detaiId,$detaiStatus,$detaiSvAccept,$svId){

     $msg = "";
     if ($svDetai == $detaiId){
         if ($detaiStatus==1){
             $msg = "Waiting";
         }else if ($detaiStatus==2){
             if ($detaiSvAccept==$svId){
                 $msg = "Accepted!";
             }else{
                 $msg = "Rejected!";
             }
         }
     }else{
         if ($detaiStatus==2){
             $msg = "Not available!";
         }else{
             $msg = "Available!";
         }
     }
     return $msg;
 }

    function create_group_dropbox($selected=0){
        global $db;
        $res = '<select name="group" id="groupbox" return false;">';
        $sql = "SELECT * from usergroups ORDER BY id ASC";
        $rows= $db->query($sql, $fetchArray=true);
        for ($i=0; $i<count($rows); $i++){
            if($i == $selected){
                $res = $res.'<option selected="selected" value="'.$rows[$i][0].'">'.$rows[$i][1].'</option>';
            }else{
                $res = $res.'<option value="'.$rows[$i][0].'">'.$rows[$i][1].'</option>';
            }
        }
        return $res.'</select>';
    }

?>

